// Admin Controller

function userEditCtrl($http, $scope, $route, $rootScope, $timeout, commonService, $location) {
    _log('### userEditCtrl ###');
    $scope.fieldData = { status: 1, usertype: 1 };
    
    var param = $location.search();
    var url = 'user_ws.php';
    var id  = '';
    
    if(param['id']){ // Edit
	id = param['id'];
            var params = {
               wsname : 'getuserbyid',
               id : param['id'],
               token : _pageData.tokenKey
            };
            $.getJSON(_COMMON.context + url, params).done(function(result) {
        	$scope.$apply(function() {
        	    $scope.fieldData = result.users;
        	});
            });
            
            $("#username").prop("readonly",true); // set readonly Edit mode
            $("#addmode").prop("hidden",true);
    }else{
	 $("#editmode").prop("hidden",true);
    }


    $scope.save = function () {

        var data = $scope.fieldData;

        if(id == ''){ // Add mode
            if($('#password').val() == ''){
        	 notif({
                     msg: "<b><i class='fa fa-thumbs-o-down'></i> Password is not blank !</b>",
                     type: "error",
                     position: "center"
                 });
        	 $('#password').focus();
                 $('#password').addClass('ng-invalid');
        	 return false;
            }else if ($('#password').val() != $('#confirmpassword').val()) {
                notif({
                    msg: "<b><i class='fa fa-thumbs-o-down'></i> Confirm Password is not match !</b>",
                    type: "error",
                    position: "center"
                });
    
                $('#confirmpassword').focus();
                $('#confirmpassword').addClass('ng-invalid');
                return false;
            }
        }else{
            _log('Edit !!!');
            if ($('#newpassword').val().trim() != $('#confirmnewpassword').val().trim()) {
                notif({
                    msg: "<b><i class='fa fa-thumbs-o-down'></i> Confirm Password is not match !</b>",
                    type: "error",
                    position: "center"
                });
    
                $('#confirmnewpassword').focus();
                $('#confirmnewpassword').addClass('ng-invalid');
                return false;
            }
        }
        
        _log(data);



        var config = {
            params: {
                wsname: id == '' ? 'createuser' : 'updateuser',
                token: _pageData.tokenKey,
                id: id,
                username: data.username ? data.username : '',
                password: id == '' ? $('#password').val() : $('#newpassword').val(),
                displayname: data.displayname ? data.displayname : '',
                email: data.email ? data.email : '',
                firstname: data.firstname ? data.firstname : '',
                lastname: data.lastname ? data.lastname : '',
                age: data.age ? data.age : '',
                birthday: $('#birthday').val(),
                usertype: $('#usertype').val(),
                status: $('#optUserActive').is(':checked') ? 1 : 0
            }
        };

        $http.post(_COMMON.context + url, null, config)
	      .success(function (obj) {
	          notif({
	              msg: "<b><i class='fa fa-thumbs-o-up'></i>  Save Success !</b>",
	              type: "success",
	              position: "center"
	          });
	          $location.path('/admin/user/list/'); 
	      })
          .error(function () {
              alert('Error is occured, May be database connect is wrong');
          });
    };
    
    $(".datepicker").datepicker({
    	dateFormat : 'dd/mm/yy',
//    	showOn : "button",
    	buttonImage : "app/images/calendar_blue.png",
//    	buttonImageOnly : false,
    	beforeShow: function(){    
    	           $(".ui-datepicker").css('font-size', 13) ;
    	    }
    });


}
